<template>
	<div class="user-page">
		<!--查询表单-->
		<el-card class="box-card mb10" shadow="never">
			<el-form ref="searchFormRef" class="search-form" :inline="true" label-position="left" label-width="75px" :model="searchForm">
				<el-row :gutter="30" justify="start">
					<el-col :span="6">
						<el-form-item class="form-item" label="角色名称:" prop="roleName">
							<el-input v-model="searchForm.roleName" placeholder="请输入角色名称" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item class="form-item" label="权限字符:" prop="roleKey">
							<el-input v-model="searchForm.roleKey" placeholder="请输入权限字符" />
						</el-form-item>
					</el-col>
					<el-col :span="6">
						<el-form-item class="form-item" label="角色状态:" prop="status">
							<el-select v-model="searchForm.status" class="el-form-item" clearable placeholder="请选择角色状态">
								<el-option label="正常" value="0" />
								<el-option label="停用" value="1" />
							</el-select>
						</el-form-item>
					</el-col>
					<template v-if="!searchMore">
						<el-col :span="6">
							<el-form-item class="form-item" label="创建日期:" prop="createTimeAry">
								<el-date-picker v-model="createTimeAry" format="YYYY-MM-DD" value-format="YYYY-MM-DD" class="el-form-item" end-placeholder="截止日期" range-separator="至" start-placeholder="开始日期" type="daterange" />
							</el-form-item>
						</el-col>
					</template>
					<el-col :span="6">
						<div class="search-btn">
							<el-button type="primary" @click="handleSearch">查询</el-button>
							<el-button @click="handleReset">重置</el-button>
							<a class="more-search" @click="searchMore = searchMore ? false : true">
								{{ searchMore ? '展开' : '收起' }}
								<el-icon>
									<component :is="searchMore ? 'ele-ArrowDown' : 'ele-ArrowUp'" color="#0052CC" />
								</el-icon>
							</a>
						</div>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
		<el-card class="box-card" shadow="never">
			<static-table :tableData="tableData" rowId="roleId" :loading="loading" :seqConfig="{ startIndex: (pageNum - 1) * pageSize }">
				<template #vux-search>
					<el-row :gutter="12" class="mb10">
						<el-button class="ml8" type="primary" v-has="'system:role:add'" @click="handleAdd">
							<el-icon><ele-Plus /></el-icon>
							&nbsp;新增
						</el-button>
					</el-row>
				</template>
				<template #vux-column>
					<vxe-column type="seq" title="序号" width="60" />
					<vxe-column field="roleName" min-width="100" show-overflow="tooltip" title="角色名称" />
					<vxe-column field="roleKey" min-width="120" show-overflow="tooltip" title="权限字符" />
					<vxe-column field="roleSort" min-width="150" title="显示顺序" />
					<vxe-column field="status" min-width="120" title="状态">
						<template #default="{ row }">
							<span>{{ row.status == '0' ? '正常' : '停用' }}</span>
							<el-switch active-color="#00B42A" inactive-color="#F04141" :model-value="row.status == '0' ? true : false" v-has="'system:role:update'" style="margin-left: 10px" @change="handleSwitch(row)" />
						</template>
					</vxe-column>
					<vxe-column field="createTime" min-width="150" title="创建时间" />
					<vxe-column field="updateTime" min-width="150" title="更新时间" />
					<vxe-column field="operation" fixed="right" min-width="120" title="操作">
						<template #default="{ row }">
							<div class="vxe-action">
								<a @click="handleUpdate(row)" v-has="'system:role:update'">修改</a>
								<a @click="handleCopy(row)" v-has="'system:role:copy'">复制</a>
								<a @click="handleDelete(row)" v-has="'system:role:delete'">删除</a>
							</div>
						</template>
					</vxe-column>
				</template>
			</static-table>
			<el-pagination background :current-page="pageNum" :layout="layout" :page-size="pageSize" :total="dataCount" @current-change="handleCurrentChange" @size-change="handleSizeChange" />
		</el-card>
	</div>
</template>
<script name="RoleManage" setup>
import global from '@/utils/global'
import useTableList from '@/hooks/useTableList'

/*
 *@Description: 状态初始化
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-08-28 10:11:35
 */
const router = useRouter()
const state = reactive({
	searchForm: {
		roleName: '',
		status: '',
		roleKey: '',
		beginTime: '',
		endTime: '',
	},
	createTimeAry: [],
	searchFormRef: null,
})
const { searchForm, createTimeAry, searchFormRef } = toRefs(state)

const queryUrl = '/meta-system/web/v1/sysRole/list'
const { loading, dataCount, searchMore, handleInitQuery, getTimeParseAry, handleModalOk, tableData, pageNum, pageSize, layout, handleSizeChange, handleCurrentChange } = useTableList(queryUrl, state.searchForm)

/*
 *@Description: 方法模块
 *@MethodAuthor: liuYuFeng
 *@Date: 2023-08-28 10:16:03
 */
const handleAdd = () => {
	router.push({
		path: '/system/role/roleAdd',
	})
}
const handleUpdate = row => {
	router.push({
		path: '/system/role/roleUpdate',
		query: {
			id: row.roleId,
		},
	})
}
const handleCopy = row => {
	router.push({
		path: '/system/role/roleCopy',
		query: {
			id: row.roleId,
		},
	})
}
const handleSearch = () => {
	const { beginTime, endTime } = getTimeParseAry(state.createTimeAry)
	state.searchForm.beginTime = beginTime
	state.searchForm.endTime = endTime
	handleInitQuery()
}
const handleReset = () => {
	state.searchFormRef.resetFields()
	state.createTimeAry = []
	handleSearch()
}
const handleDelete = row => {
	global.$baseConfirm(
		row ? `是否确认删除名称为"--【${row.roleName}】--"的数据?` : '是否确认删除当前选中的数据？',
		'提示',
		() => {
			global.$fetchPostData(`/meta-system/web/v1/sysRole/remove`, { removeIdList: [row.roleId] }).then(data => {
				if (data.code == 200) {
					global.$baseMessage(data.msg, 'success')
					handleModalOk(2)
				} else {
					global.$baseMessage(data.msg, 'error')
				}
			})
		},
		() => {}
	)
}
//switch 切换
const handleSwitch = row => {
	global.$baseConfirm(`是否确认更改名称为"--【${row.roleName}】--"的状态?`, '提示', () => {
		const user = row
		user.status = row.status == 0 ? 1 : 0
		global.$fetchPostData(`/meta-system/web/v1/sysRole/changeStatus`, user).then(data => {
			if (data.code == 200) {
				global.$baseMessage(data.msg, 'success')
				handleModalOk(2)
			} else {
				global.$baseMessage(data.msg, 'error')
			}
		})
	})
}
</script>
<style lang="scss" scoped></style>
